﻿using System.ComponentModel.DataAnnotations;
using MediatR;
using SheepCultivation.Common.Domain.main;
using SheepCultivation.Common.Domain.SheepBasicInformation;
using SheepCultivation.Common.Domain.Week2;
using SheepCultivation.Common.Dto;
using SheepCultivation.Common.Infrastructure.Interface;
using SheepCultivation.Enum;

namespace SheepCultivation.API.AddliCation.Tip_newspaper
{
    public class BackfillSeachHandler : IRequestHandler<BackfillSeachCommon, Result>
    {
        public readonly IBaseRepository<BasicInformation> BasicInformationRepository;
        public readonly IBaseRepository<Building> BuildingIBaseRepository;
        public readonly IBaseRepository<Columns> ColumnsIBaseRepository;
        public readonly IBaseRepository<Sheepbreed> SheepbreedRepository;
        public BackfillSeachHandler(IBaseRepository<BasicInformation> basicInformationRepository, IBaseRepository<Columns> 
            columnsIBaseRepository, IBaseRepository<Building> buildingIBaseRepository, IBaseRepository<Sheepbreed> sheepbreedRepository)
        {
            BasicInformationRepository = basicInformationRepository;
            ColumnsIBaseRepository = columnsIBaseRepository;
            BuildingIBaseRepository = buildingIBaseRepository;
            SheepbreedRepository = sheepbreedRepository;
        }

        public async Task<Result> Handle(BackfillSeachCommon request, CancellationToken cancellationToken)
        {
            var res = new Result() { Code = (int)CommonEnumCode.error };
            try
            {
                res.Code = (int)CommonEnumCode.success;
                var lists = (from a in BasicInformationRepository.GetAll()
                             join b in BuildingIBaseRepository.GetAll()
                             on a.BuildingId equals b.BuildingId
                             join c in ColumnsIBaseRepository.GetAll()
                             on a.ColumnsId equals c.ColumnsId
                             join d in SheepbreedRepository.GetAll()
                             on a.SheepbreedId equals d.SheepbreedId
                             where a.BreedingSheepArchiveId == request.BreedingSheepArchiveId
                             select new BackferDTO
                             {
                                 BreedingSheepArchiveId=a.BreedingSheepArchiveId,
                                 BuildingsOut =b.BuildingName,
                                 ColumnsOut=c.ColumnsName,
                                 SheepbreedName=d.SheepbreedName,
                                 SheepSex = a.SheepSex
                             }).ToList();
                //var list= TransferRepository.GetAll().FirstOrDefault(x=>x.BreedingSheepArchiveId==request.BreedingSheepArchiveId);
                res.Code = (int)CommonEnumCode.success;
                res.Data = lists;
                res.Msg = "羊只转舍反填";
            }
            catch (Exception)
            {

                throw;
            }
            return res;
        }
    }
    public class BackferDTO
    {
        /// <summary>
        /// 转舍主键
        /// </summary>
        [Key]
        public int BreedingSheepArchiveId { get; set; }
        public string BuildingsOut { get; set; }
        public string ColumnsOut { get; set; }
        public string SheepbreedName { get; set; }
        public int SheepSex { get; set; }
        public string SheepName { get; set; }
        
    }
}
